<template>
  <div class="app-container">

    <!-- 查询和其他操作 -->
    <div class="filter-container">
      <el-input v-model="listQuery.name" clearable class="filter-item" style="width: 200px;" placeholder="请输入名字" />
      <el-button v-permission="['GET /admin/cashes/approvallist']" class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查找</el-button>
    </div>

    <!-- 查询结果 -->
    <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" border fit highlight-current-row>
      <el-table-column align="center" label="提现人名" prop="name" sortable />
      <el-table-column align="center" label="银行卡号" prop="cardNo" sortable />
      <el-table-column align="center" label="提现金额" prop="money" sortable />
      <el-table-column align="center" label="银行名" prop="bankName" sortable />
      <el-table-column align="center" label="状态" prop="cashStatus" sortable>
        <template slot-scope="scope">
          <span v-html="statusFormatter(scope)" />
        </template>
      </el-table-column>
      <el-table-column align="center" label="拒绝信息" prop="refuseInfo" sortable />
      <el-table-column align="center" label="操作" width="250" class-name="oper">
        <template slot-scope="scope">
          <el-button :disabled="scope.row.cashStatus!=0" type="primary" size="mini" @click="handleApproval(scope.row)">审批</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="dataForm" :rules="rules" :model="dataForm" status-icon label-position="left" label-width="100px" style="width: 400px; margin-left:50px;">

        <el-form-item label="当前余额">
          <el-input v-model.number="balance" disabled>
            <template slot="prepend">￥</template>
            <template slot="append">元</template>
          </el-input>

        </el-form-item>
        <el-form-item label="提现人名" prop="name">
          <el-input v-model="dataForm.name" disabled />
        </el-form-item>
        <el-form-item label="银行卡号" prop="cardNo">
          <el-input v-model="dataForm.cardNo"disabled />
        </el-form-item>
        <el-form-item label="银行名" prop="bankName">
          <el-input v-model="dataForm.bankName" disabled />
        </el-form-item>
        <el-form-item label="提现余额" prop="money">
          <el-input v-model.number="dataForm.money" disabled>
            <template slot="prepend">￥</template>
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="审批结果" disabled prop="remark">
          <el-radio v-model="dataForm.cashStatus" :label="0">提现申请</el-radio>
          <el-radio v-model="dataForm.cashStatus" :label="1">提现成功</el-radio>
          <el-radio v-model="dataForm.cashStatus" :label="2">拒绝提现</el-radio>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="dataForm.refuseInfo" :disabled="dataForm.cashStatus!=2" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="approval">审批</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #20a0ff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 120px;
  height: 120px;
  line-height: 120px;
  text-align: center;
}
.avatar {
  width: 145px;
  height: 145px;
  display: block;
}
</style>

<script>
import { approvallist, approval } from '@/api/cashes'
import { uploadPath } from '@/api/storage'
import { queryBalance } from '@/api/store'
import { getToken } from '@/utils/auth'
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination

export default {
  name: 'Cashes',
  components: { Pagination },
  data() {
    return {
      uploadPath,
      list: [],
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        content: undefined,
        sort: 'add_time',
        order: 'desc'
      },
      dataForm: {
        money: '',
        name: '',
        cardNo: '',
        bankName: ''
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '创建'
      },
      rules: {

        money: [
          { required: true, message: '余额不能为空', trigger: 'blur' }
        ],
        refuseInfo: [
          { required: true, message: '拒绝信息不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '提现人名不能为空', trigger: 'blur' }
        ],

        cardNo: [
          { required: true, message: '银行卡号不能为空', trigger: 'blur' }
        ],
        bankName: [
          { required: true, message: '银行名不能为空', trigger: 'blur' }
        ],
        remark: [
          { required: true, message: 'remark不能为空', trigger: 'blur' }
        ]
      },
      downloadLoading: false,
      balance: 0
    }
  },
  computed: {
    headers() {
      return {
        'X-Litemall-Admin-Token': getToken()
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    statusFormatter(scope) {
      if (scope.row.cashStatus == 0) {
        return "<span style='color:green'>提现申请</span>"
      }
      if (scope.row.cashStatus == 1) {
        return "<span style='color:pink'>提现成功</span>"
      }
      if (scope.row.cashStatus == 2) {
        return "<span style='color:pink'>拒绝提现</span>"
      }
      return "<span style='color:red'>未知</span>"
    },
    getList() {
      this.listLoading = true
      approvallist(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
          this.listLoading = false
        })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    resetForm() {
      this.dataForm = {
        money: undefined,
        refuseInfo: undefined,
        name: undefined,
        cashStatus: undefined,
        cardNo: undefined,
        bankName: undefined,
        remark: undefined
      }
    },
    uploadUrl: function(response) {
      this.dataForm.url = response.data.url
    },
    checkFileSize: function(file) {
      if (file.size > 1048576) {
        this.$message.error(file.name + '文件大于1024KB，请选择小于1024KB大小的图片')
        return false
      }
      return true
    },

    handleApproval(row) {
      this.resetForm()
      this.dataForm = Object.assign({}, row)
      this.dialogStatus = 'approval'
      this.dialogFormVisible = true
    },
    approval(row) {
      approval(this.dataForm)
        .then(response => {
          this.getList()
          this.dialogFormVisible = false
          this.$notify.success({
            title: '成功',
            message: '创建成功'
          })
        })
        .catch(response => {
          this.$notify.error({
            title: '失败',
            message: response.data.errmsg
          })
        })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = [
          '商户ID',
          '余额',
          '拒绝信息',
          '提现人名',
          '状态0 提现申请 1提现成功 2拒绝提现',
          '银行卡号',
          '银行名',
          'remark',
          '用户ID'
        ]
        const filterVal = [
          'storeId',
          'money',
          'refuseInfo',
          'name',
          'cashStatus',
          'cardNo',
          'bankName',
          'remark',
          'userId'
        ]
        excel.export_json_to_excel2(tHeader, this.list, filterVal, '广告信息')
        this.downloadLoading = false
      })
    }
  }
}
</script>
